package sourismobile.MIDlet.core;

import java.io.IOException;
import java.lang.String;

import javax.bluetooth.*;
import javax.microedition.io.*;


public class ServiceSeeker {

	SourisMobileControler midlet;
	LocalDevice local;
	DiscoveryAgent discoveryAgent;
	String connectionString;
	StreamConnection connection;
	
	public ServiceSeeker(SourisMobileControler midlet) {
		try {
			this.midlet = midlet;
			local = LocalDevice.getLocalDevice();
			discoveryAgent = local.getDiscoveryAgent();
		} catch (BluetoothStateException e) {
			midlet.throwsError(e.getMessage());
		}
	} 
	
	public String getConnectionString() {
		return connectionString;
	}
	public StreamConnection getConnection() {
		return connection;
	}
	
	public boolean lookingForService() {
		try {
			connectionString = discoveryAgent.selectService(new UUID("86b4d249fb8844d6a756ec265dd1f6a4", false),ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false);
		} catch (BluetoothStateException e) {
			midlet.throwsError(e.getMessage());	
		}
		if (connectionString != null) return true;
		else return false;
	}
	
	public boolean connectionToService() {
		if (connectionString == null) return false;
		try {
			connection = (StreamConnection) Connector.open(connectionString);
		} catch (IOException e) {
			midlet.throwsError(e.getMessage());	
		}
		if (connection != null) return true;
		return false;
	}
}
